package com.honeycomb.action.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.google.gson.JsonObject;
import com.honeycomb.action.BaseAjaxAction;
import com.honeycomb.service.IUserService;
import com.honeycomb.utils.ErrorMsgConstants;
import com.honeycomb.utils.SystemConstants;
import com.honeycomb.vo.BaseVO;
import com.honeycomb.vo.ResultDto;
import com.honeycomb.vo.UserInfoVO;

/**
 * 
 * @author ZhongJiang
 * 
 * @function AJAX请求用户信息
 * 
 * @param 用户ID
 * 
 * @return json
 * 
 */
public class AjaxFindUserInfoByIdAction extends BaseAjaxAction {
	private IUserService iUserService;
	
	public IUserService getiUserService() {
		return iUserService;
	}
	public void setiUserService(IUserService iUserService) {
		this.iUserService = iUserService;
	}

	@Override
	protected JSONObject doRequest(HttpServletRequest request,
			HttpServletResponse response, HttpSession session) throws Exception {
		logger.info("---------------AjaxFindUserInfoByIdAction-----------------");
		JSONObject jo = new JSONObject();
		JSONObject joo = null;
		JSONArray ja = null;
		String userInfoId = (String)request.getParameter("userInfoId");
		if(userInfoId == null || userInfoId.equals("") || userInfoId.equals("null")) {
			jo.put("result", SystemConstants.AJAX_FAILED_ALERT);
			jo.put("msg", ErrorMsgConstants.ERR_LOGIN);
			return jo;
		}
		ResultDto<BaseVO> resultDtoFindUserInfo = null;
		UserInfoVO userInfoVO = null;
		try {
			resultDtoFindUserInfo = this.getiUserService().doQueryUserInfo(userInfoId);
			if(resultDtoFindUserInfo.isSuccess()) {
				userInfoVO = (UserInfoVO)resultDtoFindUserInfo.getFirstResult();
				joo = new JSONObject();
				joo.put("userId", userInfoVO.getUserId());
				joo.put("userFaceImgUrl", userInfoVO.getUserHeadImgUrl());
				joo.put("userNickname", userInfoVO.getUserNickname());
				joo.put("userSex", userInfoVO.getUserSex());
			}
			ja = new JSONArray();
			ja.add(joo);
			jo.put("result", SystemConstants.AJAX_SUCCESS);
			jo.put("list", ja);
			return jo;
		} catch (Exception e) {
			e.printStackTrace();
			jo.put("result", SystemConstants.AJAX_FAILED_ALERT);
			jo.put("msg", e.getMessage());
			return jo;
		}
	}

}
